<template><div><h2 id="错误处理" tabindex="-1"><a class="header-anchor" href="#错误处理"><span>错误处理</span></a></h2>
<p>Yii 中文文档 /</p>
<h2 id="错误处理-1" tabindex="-1"><a class="header-anchor" href="#错误处理-1"><span>错误处理</span></a></h2>
<p>在处理 RESTful API 请求时，如果用户请求中出现错误或服务器上发生意外情况，您可以简单地抛出异常来通知用户出了问题。如果可以确定错误的原因（例如，请求的资源不存在），则应考虑抛出异常以及正确的 HTTP 状态代码（例如，<code v-pre>yii\web\NotFoundHttpException</code> 表示 <code v-pre>404</code> 状态代码）。Yii 将发送响应以及相应的 HTTP 状态代码和文本。Yii 还将在响应正文中包含异常的序列化表示。例如<br>
。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token constant">HTTP</span><span class="token operator">/</span><span class="token number">1.1</span> <span class="token number">404</span> Not Found</span>
<span class="line">Date<span class="token punctuation">:</span> Sun<span class="token punctuation">,</span> <span class="token number">02</span> Mar <span class="token number">2014</span> <span class="token number">05</span><span class="token punctuation">:</span><span class="token number">31</span><span class="token punctuation">:</span><span class="token number">43</span> <span class="token constant">GMT</span></span>
<span class="line">Server<span class="token punctuation">:</span> Apache<span class="token operator">/</span><span class="token number">2.2</span><span class="token number">.26</span> <span class="token punctuation">(</span>Unix<span class="token punctuation">)</span> <span class="token constant">DAV</span><span class="token operator">/</span><span class="token number">2</span> <span class="token constant">PHP</span><span class="token operator">/</span><span class="token number">5.4</span><span class="token number">.20</span> mod_ssl<span class="token operator">/</span><span class="token number">2.2</span><span class="token number">.26</span> OpenSSL<span class="token operator">/</span><span class="token number">0.9</span><span class="token number">.8</span>y</span>
<span class="line">Transfer<span class="token operator">-</span>Encoding<span class="token punctuation">:</span> chunked</span>
<span class="line">Content<span class="token operator">-</span>Type<span class="token punctuation">:</span> application<span class="token operator">/</span>json<span class="token punctuation">;</span> charset<span class="token operator">=</span><span class="token constant">UTF</span><span class="token operator">-</span><span class="token number">8</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"name"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Not Found Exception"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string double-quoted-string">"message"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"The requested resource was not found."</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string double-quoted-string">"code"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string double-quoted-string">"status"</span><span class="token punctuation">:</span> <span class="token number">404</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以下列表总结了Yii REST框架使用的HTTP状态代码：</p>
<ul>
<li><code v-pre>200</code>: 好的，一切都按预期进行。</li>
<li><code v-pre>201</code>: 已成功创建资源以响应 POST 请求。 Location 标头包含指向新创建的资源的 URL。</li>
<li><code v-pre>204</code>: 请求已成功处理，响应不包含正文内容（如 DELETE 请求）。</li>
<li><code v-pre>304</code>: 未修改资源。您可以使用缓存版本。</li>
<li><code v-pre>400</code>: 错误的请求。这可能是由用户的各种操作引起的，例如在请求正文中提供无效的 JSON 数据、提供无效的操作参数等。</li>
<li><code v-pre>401</code>: 身份验证失败。</li>
<li><code v-pre>403</code>: 不允许经过身份验证的用户访问指定的 API 终端节点。</li>
<li><code v-pre>404</code>: 请求的资源不存在。</li>
<li><code v-pre>405</code>: 不允许使用方法。请检查标 Allow 头中允许的 HTTP 方法。</li>
<li><code v-pre>415</code>: 不支持的媒体类型。请求的内容类型或版本号无效。</li>
<li><code v-pre>422</code>: 数据验证失败（例如，响应 POST 请求时）。请查看响应正文以获取详细的错误消息。</li>
<li><code v-pre>429</code>: 请求过多。由于速率限制，请求被拒绝。</li>
<li><code v-pre>500</code>: 内部服务器错误。这可能是由内部程序错误引起的。</li>
</ul>
<h2 id="自定义错误响应" tabindex="-1"><a class="header-anchor" href="#自定义错误响应"><span>自定义错误响应</span></a></h2>
<p>有时，您可能希望自定义默认错误响应格式。例如，您希望始终使用 200 作为 HTTP 状态，并将实际的 HTTP 状态代码作为响应中 JSON 结构的一部分，而不是依赖使用不同的 HTTP 状态来指示不同的错误，如下所示：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token constant">HTTP</span><span class="token operator">/</span><span class="token number">1.1</span> <span class="token number">200</span> <span class="token constant">OK</span></span>
<span class="line">Date<span class="token punctuation">:</span> Sun<span class="token punctuation">,</span> <span class="token number">02</span> Mar <span class="token number">2014</span> <span class="token number">05</span><span class="token punctuation">:</span><span class="token number">31</span><span class="token punctuation">:</span><span class="token number">43</span> <span class="token constant">GMT</span></span>
<span class="line">Server<span class="token punctuation">:</span> Apache<span class="token operator">/</span><span class="token number">2.2</span><span class="token number">.26</span> <span class="token punctuation">(</span>Unix<span class="token punctuation">)</span> <span class="token constant">DAV</span><span class="token operator">/</span><span class="token number">2</span> <span class="token constant">PHP</span><span class="token operator">/</span><span class="token number">5.4</span><span class="token number">.20</span> mod_ssl<span class="token operator">/</span><span class="token number">2.2</span><span class="token number">.26</span> OpenSSL<span class="token operator">/</span><span class="token number">0.9</span><span class="token number">.8</span>y</span>
<span class="line">Transfer<span class="token operator">-</span>Encoding<span class="token punctuation">:</span> chunked</span>
<span class="line">Content<span class="token operator">-</span>Type<span class="token punctuation">:</span> application<span class="token operator">/</span>json<span class="token punctuation">;</span> charset<span class="token operator">=</span><span class="token constant">UTF</span><span class="token operator">-</span><span class="token number">8</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"success"</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string double-quoted-string">"data"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"name"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"Not Found Exception"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"message"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"The requested resource was not found."</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"code"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"status"</span><span class="token punctuation">:</span> <span class="token number">404</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要实现此目标，您可以在应用程序配置中响应 <code v-pre>response</code> 组件 <code v-pre>beforeSend</code> 的事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'response'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\web\Response'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'on beforeSend'</span> <span class="token operator">=></span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">sender</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span> <span class="token operator">!==</span> <span class="token constant">null</span> <span class="token operator">&amp;&amp;</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">request</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'suppress_response_code'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'success'</span> <span class="token operator">=></span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">isSuccessful</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'data'</span> <span class="token operator">=></span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">data</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">                    <span class="token variable">$response</span><span class="token operator">-></span><span class="token property">statusCode</span> <span class="token operator">=</span> <span class="token number">200</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面的代码将重新格式化响应（对于成功和失败的响应），如在作为 <code v-pre>GET</code> 参数传递时 <code v-pre>suppress_response_code</code>所解释的那样。</p>
<blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


